﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input.ManageInput;
using IntelligentAgriculture.Contracts.Dto.Input.RidgepInput;
using IntelligentAgriculture.Contracts.Dto.Output.EarTagOutput;
using IntelligentAgriculture.Contracts.Dto.Output.ManageOutput;
using IntelligentAgriculture.Contracts.Dto.Output.PageOutput;
using IntelligentAgriculture.Contracts.Dto.Output.RidgepOutPut;
using IntelligentAgriculture.Contracts.Interface.Iridgepletype;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Impl.Managements;
using IntelligentAgriculture.Domain.Interface.IEarTagReposiotry;
using IntelligentAgriculture.Domain.Interface.IManagement;
using IntelligentAgriculture.Domain.Interface.Iridgepletype;
using IntelligentAgriculture.Services.Management;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services.Ridegp_eService
{
    public class RidegpoleService : IridegpoleService
    {
        private readonly IridegRepository _iridegRepository;
        private readonly ILogger<RidegpoleService> _logger;

        private readonly IMapper _mapper;
        public RidegpoleService(IridegRepository iridegRepository, ILogger<RidegpoleService> logger, IMapper mapper)
        {
            _iridegRepository = iridegRepository;
            _logger = logger;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="entity">方法参数</param>
        /// <returns>返回受影响行数</returns>
     
        public async Task<int> Add(Ridgepole entity)
        {
            try
            {
                var AddRidgep = _mapper.Map<Domain.Entity.Ridgepole>(entity);
                return await _iridegRepository.Add(AddRidgep);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据指定的条件获取栋舍展示信息，查询，分页返回结果
        /// </summary>
        /// <param name="input">包含获取展示信息所需的输入参数</param>
        /// <returns>返回一个包含展示信息和分页信息的对象</returns>
        public PagesOutput<RidgepShowOutput> GetShow(RidgepShowinput input)
        {

            try
            {
                var list = _iridegRepository.Showinfo().AsNoTracking();
                if (input.RidgepoleTypeId > 0)
                {
                    list = list.Where(x => x.RidgepoleTypeId == input.RidgepoleTypeId);
                }
                if (!string.IsNullOrEmpty(input.RidgepoleName))
                {
                    list = list.Where(x => x.RidgepoleName.Contains(input.RidgepoleName));
                }
                if (!string.IsNullOrEmpty(input.ResponsiblePerson))
                {
                    list = list.Where(x => x.ResponsiblePerson.Contains(input.ResponsiblePerson));
                }

                var result = new PagesOutput<RidgepShowOutput>()
                {
                    Total = list.Count(),
                    PageSize = input.PageSize
                };

                var query = list.OrderByDescending(x => x.RidgepoleId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
                result.Data = _mapper.Map<List<RidgepShowOutput>>(query);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改指定的栋舍信息
        /// </summary>
        /// <param name="entity">包含更新信息的输入实体</param>
        /// <returns>返回一个表示操作成功的异步任务，返回更新的记录数</returns>
        public async Task<int> Update(Ridgepole entity)
        {
            try
            {

                var updatedManager = _mapper.Map<Ridgepole>(entity);
                return await _iridegRepository.Update(updatedManager);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据指定的ID删除单个栋舍信息
        /// </summary>
        /// <param name="id">要删除的耳标信息的ID</param>
        /// <returns>返回一个表示操作成功的异步任务，返回删除的记录数</returns>
        async Task<int> IridegpoleService.Delete(int id)
        {
            try
            {
                return await _iridegRepository.Delete(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除指定IDs的栋舍信息
        /// </summary>
        /// <param name="ids">要删除的耳标信息的ID集合。</param>
        /// <returns>返回一个表示操作成功的异步任务，返回删除的记录数。</returns>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            try
            {
                return await _iridegRepository.DeleteRange(ids);
            }
            catch (Exception)
            {

                throw;
            }
        }
       /// <summary>
       /// 反填功能
       /// </summary>
       /// <param name="id">ID</param>
       /// <returns>通过ID反填列表</returns>
        public async Task<RidgepShowOutput> Find(int id)
        {
            try
            {
                

                var list = await _iridegRepository.Find(id);
                if(list==null)
                {
                    return null;
                }
                var lists = _mapper.Map<RidgepShowOutput>(list);
                return lists;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id获取栋舍
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
		public async Task<IQueryable<Ridgepole>> GetRidgepoles(int rid)
        {
            try
            {
                var list = _iridegRepository.Showinfo();
                if (rid > 0)
                {
                    list = list.Where(x => x.RidgepoleId == rid);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
	}
}
